<?php


namespace app\api\v1\graphQLModel;


use app\api\model\ApiListModel;
use app\api\v1\graphQLModel\fieldType\ApiListFieldType;
use GraphQL\Type\Definition\ResolveInfo;
use iflow\Swoole\GraphQL\Annotation\Type\lib\ArgsType;
use iflow\Swoole\GraphQL\Annotation\Type\lib\fieldType;
use iflow\Swoole\GraphQL\Annotation\Type\lib\resolveType;
use iflow\Swoole\GraphQL\Annotation\Type\TypeAnnotation;

#[TypeAnnotation('community')]
class CommunityGraphQL
{

    /**
     * 获取 开放 API  列表
     * @var array
     */
    #[fieldType('apiList', ApiListFieldType::class)]
    #[ArgsType([
        'page' => [
            'type' => 'nonNull->int'
        ],
        'keyword' => [
            'type' => 'string'
        ]
    ])]
    #[resolveType(CommunityGraphQL::class.'@getApiList')]
    public array $apiList = [];


    /**
     * 获取当前系统API列表
     * @param $rootValue
     * @param array $args
     * @param $context
     * @param ResolveInfo $resolveInfo
     * @return array
     */
    public function getApiList($rootValue, array $args, $context, ResolveInfo $resolveInfo): array
    {
        $info = (new ApiListModel()) -> getApiList(
            $args['page'], $args['keyword'] ?? ''
        ) -> data;

        $info['api_list'] = $info['items'];
        return $info;
    }
}